﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Apps.IBLL;
using Apps.Models;
using Apps.IDAL;
using Apps.Models.Sys;
using Apps.BLL.Core;
using Apps.Common;
using System.Linq.Expressions;

namespace Apps.BLL
{

    public class HomeBLL : IHomeBLL
    {
        [Dependency]
        public IHomeRepository HomeRepository { get; set; }
        public List<SysModuleModel> GetMenuByPersonId(string personId, string moduleId)
        {
            IQueryable<SysModule> queryData = HomeRepository.GetMenuByPersonId(personId, moduleId);
            return CreateModelList(ref queryData);
        }

        private List<SysModuleModel> CreateModelList(ref IQueryable<SysModule> queryData)
        {
            List<SysModuleModel> modelList = (from r in queryData
                                              select new SysModuleModel
                                              {
                                                  Id = r.Id,
                                                  Name = r.Name,
                                                  EnglishName = r.EnglishName,
                                                  ParentId = r.ParentId,
                                                  Url = r.Url,
                                                  Iconic = r.Iconic,
                                                  Sort = r.Sort,
                                                  Remark = r.Remark,
                                                  Enable = r.Enable,
                                                  CreatePerson = r.CreatePerson,
                                                  CreateTime = r.CreateTime,
                                                  IsLast = r.IsLast
                                              }).ToList();
            return modelList;
        }

        public V_HP_CALORIFIC_DIFFERENCE QueryCALORIFIC_DIFFERENCE()
        {
            return HomeRepository.QueryCALORIFIC_DIFFERENCE();
        }

        public List<V_HP_BLANCE_EXPEND> QueryBlanceExpendByYear(int iYear)
        {
            var queryData = HomeRepository.QueryBlanceExpend();
            var Temp = queryData.Where(p => p.AXIS_YEAR == iYear)
                .OrderBy(o => o.AXIS_MONTH)
                .GroupBy(x => new { x.AXIS_YEAR, x.AXIS_MONTH })
                .Select(g => new
                {
                    AXIS_YEAR = g.Key.AXIS_YEAR,
                    AXIS_MONTH = g.Key.AXIS_MONTH,
                    BLANCE = g.Sum(x => x.BLANCE),
                    EXPEND = g.Sum(x => x.EXPEND)
                }).ToList();

            List<V_HP_BLANCE_EXPEND> result = (from u in Temp
                                               orderby u.AXIS_MONTH ascending
                                               select new V_HP_BLANCE_EXPEND()
                                               {
                                                   AXIS_YEAR = u.AXIS_YEAR,
                                                   AXIS_MONTH = u.AXIS_MONTH,
                                                   BLANCE = u.BLANCE,
                                                   EXPEND = u.EXPEND
                                               }).ToList();

            return result;
        }

        public List<V_HP_BLANCE_EXPEND> QueryBlanceExpendByMonth(int iYear, int iMonth)
        {
            var queryData = HomeRepository.QueryBlanceExpend();
            var Temp = queryData.Where(p => p.AXIS_YEAR == iYear && p.AXIS_MONTH == iMonth)
                .OrderBy(o => o.AXIS_DAY)
                .GroupBy(x => new { x.AXIS_DATE, x.AXIS_DAY })
                .Select(g => new
                {
                    AXIS_DATE = g.Key.AXIS_DATE,
                    AXIS_DAY = g.Key.AXIS_DAY,
                    BLANCE = g.Sum(x => x.BLANCE),
                    EXPEND = g.Sum(x => x.EXPEND)
                }).ToList();

            List<V_HP_BLANCE_EXPEND> result = (from u in Temp
                                               orderby u.AXIS_DATE ascending
                                               select new V_HP_BLANCE_EXPEND()
                                               {
                                                   AXIS_DATE = u.AXIS_DATE,
                                                   AXIS_DAY = u.AXIS_DAY,
                                                   BLANCE = u.BLANCE,
                                                   EXPEND = u.EXPEND
                                               }).ToList();

            return result;
        }

        public List<V_RPT_JHC_MONTH> QueryJHCMonth(int Year)
        {

            var queryData = HomeRepository.QueryJHCMonth();
            var Temp = queryData.Where(p => p.RPT_YEAR == Year)
                .OrderBy(o => o.RPT_MONTH)
                .GroupBy(x => new { x.RPT_YEAR, x.RPT_MONTH })
                .Select(g => new
                {
                    RPT_YEAR = g.Key.RPT_YEAR,
                    RPT_MONTH = g.Key.RPT_MONTH,
                    RPT_IN = g.Sum(x => x.RPT_IN),
                    RPT_OUT = g.Sum(x => x.RPT_OUT),
                    RPT_STOCK = g.Sum(x => x.RPT_STOCK)
                }).ToList();

            List<V_RPT_JHC_MONTH> result = (from u in Temp
                                            orderby u.RPT_MONTH ascending
                                            select new V_RPT_JHC_MONTH()
                                            {
                                                RPT_YEAR = u.RPT_YEAR,
                                                RPT_MONTH = u.RPT_MONTH,
                                                RPT_IN = u.RPT_IN,
                                                RPT_OUT = u.RPT_OUT,
                                                RPT_STOCK = u.RPT_STOCK
                                            }).ToList();

            return result;
        }

        public List<V_RPT_JHC_DAY> QueryJHCDay(int iYear, int iMonth)
        {

            var queryData = HomeRepository.QueryJHCDay();
            var Temp = queryData.Where(p => p.RPT_YEAR == iYear && p.RPT_MONTH == iMonth)
                .OrderBy(o => o.RPT_DATE)
                .GroupBy(x => new { x.RPT_DATE })
                .Select(g => new
                {
                    RPT_DATE = g.Key.RPT_DATE,
                    RPT_IN = g.Sum(x => x.RPT_IN),
                    RPT_OUT = g.Sum(x => x.RPT_OUT),
                    RPT_STOCK = g.Sum(x => x.RPT_STOCK)
                }).ToList();

            List<V_RPT_JHC_DAY> result = (from u in Temp
                                          orderby u.RPT_DATE ascending
                                          select new V_RPT_JHC_DAY()
                                          {
                                              RPT_DATE = u.RPT_DATE,
                                              RPT_IN = u.RPT_IN,
                                              RPT_OUT = u.RPT_OUT,
                                              RPT_STOCK = u.RPT_STOCK
                                          }).ToList();

            return result;
        }

        public List<V_RPT_RCM_DETAIL> QueryRCMDetail(int iYear, int iMonth)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RPT_RCM> queryData = null;
                if (iMonth == 0)
                {
                    queryData = db.Database.SqlQuery<V_RPT_RCM>(string.Format(@"SELECT STATS_MONTH AS STATS_DATE,
                                                                                        0 as BILL_WEIGHT,
                                                                                       CASE
                                                                                         WHEN CARCOUNT IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          CARCOUNT
                                                                                       END AS CARCOUNT,
                                                                                       CASE
                                                                                         WHEN BALANCE IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          BALANCE
                                                                                       END AS BALANCE
                                                                                  FROM (SELECT A.F_MONTH AS STATS_MONTH, B.CARCOUNT, B.BALANCE
                                                                                          FROM PUB_MONTH A
                                                                                          LEFT JOIN (SELECT STATS_YEAR,
                                                                                                           STATS_MONTH,
                                                                                                           COUNT(1) AS CARCOUNT,
                                                                                                           SUM(BALANCE) AS BALANCE
                                                                                                      FROM V_RPT_RCM_DETAIL
                                                                                                     WHERE STATS_YEAR = {0}
                                                                                                     GROUP BY STATS_YEAR, STATS_MONTH) B
                                                                                            ON A.F_YEAR = B.STATS_YEAR
                                                                                           AND A.F_MONTH = B.STATS_MONTH
                                                                                         WHERE A.F_YEAR = {0}
                                                                                         ORDER BY A.F_MONTH ASC)", iYear)).AsQueryable();
                }
                else
                {
                    queryData = db.Database.SqlQuery<V_RPT_RCM>(string.Format(@"SELECT STATS_DAY AS STATS_DATE,
                                                                                        0 as BILL_WEIGHT,
                                                                                        CASE
                                                                                            WHEN CARCOUNT IS NULL THEN
                                                                                            0
                                                                                            ELSE
                                                                                            CARCOUNT
                                                                                        END AS CARCOUNT,
                                                                                        CASE
                                                                                            WHEN BALANCE IS NULL THEN
                                                                                            0
                                                                                            ELSE
                                                                                            BALANCE
                                                                                        END AS BALANCE
                                                                                    FROM (SELECT A.F_DAY AS STATS_DAY, B.CARCOUNT, B.BALANCE
                                                                                            FROM PUB_DAY A
                                                                                            LEFT JOIN (SELECT STATS_YEAR,
                                                                                                            STATS_MONTH,
                                                                                                            STATS_DAY,
                                                                                                            COUNT(1) AS CARCOUNT,
                                                                                                            SUM(BALANCE) AS BALANCE
                                                                                                        FROM V_RPT_RCM_DETAIL
                                                                                                        WHERE STATS_YEAR = {0}
                                                                                                        AND STATS_MONTH = {1}
                                                                                                        GROUP BY STATS_YEAR, STATS_MONTH, STATS_DAY) B
                                                                                            ON A.F_YEAR = B.STATS_YEAR
                                                                                            AND A.F_MONTH = B.STATS_MONTH
                                                                                            AND A.F_DAY = B.STATS_DAY
                                                                                            WHERE A.F_YEAR = {0}
                                                                                            AND A.F_MONTH = {1})
                                                                                    ORDER BY STATS_DAY ASC", iYear, iMonth)).AsQueryable();
                }

                List<V_RPT_RCM_DETAIL> result = (from u in queryData.ToList()
                                                 orderby u.STATS_DATE ascending
                                                 select new V_RPT_RCM_DETAIL()
                                                 {
                                                     STATS_MONTH = u.STATS_DATE,
                                                     STATS_DAY = u.STATS_DATE,
                                                     CUTBILL = (decimal)u.CARCOUNT,
                                                     BALANCE = (decimal)u.BALANCE
                                                 }).ToList();

                return result;

            }
        }

        public List<V_RPT_RCM_DETAIL> QueryRCMDetail(Expression<Func<V_RPT_RCM_DETAIL, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RPT_RCM_DETAIL> queryData = db.V_RPT_RCM_DETAIL.Where(whereLambda.Compile()).AsQueryable();
                //排序
                return queryData.ToList();

            }
        }

        public List<V_RPT_RCM_DETAIL> QueryRCMDetailChart(int Year, int Month, string MaterielId, string SupplierId, string TransportId)
        {

            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RPT_RCM> queryData = null;
                if (!string.IsNullOrEmpty(MaterielId) && !string.IsNullOrEmpty(SupplierId) && !string.IsNullOrEmpty(TransportId))
                {
                    queryData = db.Database.SqlQuery<V_RPT_RCM>(string.Format(@"SELECT STATS_DAY AS STATS_DATE,
                                                                                       CASE
                                                                                         WHEN BILL_WEIGHT IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          BILL_WEIGHT
                                                                                       END AS BILL_WEIGHT,
                                                                                       CASE
                                                                                         WHEN CARCOUNT IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          CARCOUNT
                                                                                       END AS CARCOUNT,
                                                                                       CASE
                                                                                         WHEN BALANCE IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          BALANCE
                                                                                       END AS BALANCE
                                                                                  FROM (SELECT A.F_DAY AS STATS_DAY, B.BILL_WEIGHT, B.CARCOUNT, B.BALANCE
                                                                                          FROM PUB_DAY A
                                                                                          LEFT JOIN (SELECT STATS_YEAR,
                                                                                                           STATS_MONTH,
                                                                                                           STATS_DAY,
                                                                                                           SUM(BILL_WEIGHT) AS BILL_WEIGHT,
                                                                                                           COUNT(1) AS CARCOUNT,
                                                                                                           SUM(BALANCE) AS BALANCE
                                                                                                      FROM V_RPT_RCM_DETAIL
                                                                                                     WHERE STATS_YEAR = {0}
                                                                                                       AND STATS_MONTH = {1}
                                                                                                       AND MATERIEL_ID = '{2}'
                                                                                                       AND SUPPLIER_ID = '{3}'
                                                                                                       AND TRANSPORT_ID = '{4}'
                                                                                                     GROUP BY STATS_YEAR, STATS_MONTH, STATS_DAY) B
                                                                                            ON A.F_YEAR = B.STATS_YEAR
                                                                                           AND A.F_MONTH = B.STATS_MONTH
                                                                                           AND A.F_DAY = B.STATS_DAY
                                                                                         WHERE A.F_YEAR = {0}
                                                                                           AND A.F_MONTH = {1})
                                                                                 ORDER BY STATS_DAY ASC", Year, Month, MaterielId, SupplierId, TransportId)).AsQueryable();
                }
                else
                {
                    queryData = db.Database.SqlQuery<V_RPT_RCM>(string.Format(@"SELECT STATS_DAY AS STATS_DATE,
                                                                                       CASE
                                                                                         WHEN BILL_WEIGHT IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          BILL_WEIGHT
                                                                                       END AS BILL_WEIGHT,
                                                                                       CASE
                                                                                         WHEN CARCOUNT IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          CARCOUNT
                                                                                       END AS CARCOUNT,
                                                                                       CASE
                                                                                         WHEN BALANCE IS NULL THEN
                                                                                          0
                                                                                         ELSE
                                                                                          BALANCE
                                                                                       END AS BALANCE
                                                                                  FROM (SELECT A.F_DAY AS STATS_DAY, B.BILL_WEIGHT, B.CARCOUNT, B.BALANCE
                                                                                          FROM PUB_DAY A
                                                                                          LEFT JOIN (SELECT STATS_YEAR,
                                                                                                           STATS_MONTH,
                                                                                                           STATS_DAY,
                                                                                                           SUM(BILL_WEIGHT) AS BILL_WEIGHT,
                                                                                                           COUNT(1) AS CARCOUNT,
                                                                                                           SUM(BALANCE) AS BALANCE
                                                                                                      FROM V_RPT_RCM_DETAIL
                                                                                                     WHERE STATS_YEAR = {0}
                                                                                                       AND STATS_MONTH = {1}
                                                                                                     GROUP BY STATS_YEAR, STATS_MONTH, STATS_DAY) B
                                                                                            ON A.F_YEAR = B.STATS_YEAR
                                                                                           AND A.F_MONTH = B.STATS_MONTH
                                                                                           AND A.F_DAY = B.STATS_DAY
                                                                                         WHERE A.F_YEAR = {0}
                                                                                           AND A.F_MONTH = {1})
                                                                                 ORDER BY STATS_DAY ASC", Year, Month)).AsQueryable();
                }

                List<V_RPT_RCM_DETAIL> result = (from u in queryData.ToList()
                                                 orderby u.STATS_DATE ascending
                                                 select new V_RPT_RCM_DETAIL()
                                                 {
                                                     STATS_MONTH = u.STATS_DATE,
                                                     STATS_DAY = u.STATS_DATE,
                                                     BILL_WEIGHT = u.BILL_WEIGHT,
                                                     CUTBILL = (decimal)u.CARCOUNT,
                                                     BALANCE = (decimal)u.BALANCE
                                                 }).ToList();

                return result;

            }
        }

        public List<V_RPT_RCM_DETAIL> QueryRCMDetailChart(int QueryYear, int QueryMonth, string LPR)
        {
            List<V_RPT_RCM_DETAIL> result = new List<V_RPT_RCM_DETAIL>();
            var queryData = HomeRepository.QueryRCMDetail().Where(p => p.STATS_YEAR == QueryYear && p.STATS_MONTH == QueryMonth && p.LPR == LPR);

            var Temp = queryData.OrderBy(o => new { o.PLAN_DATE })
                .Select(g => new
                {
                    PLAN_DATE = g.PLAN_DATE,
                    CAR_ID = g.CAR_ID,
                    LPR = g.LPR,
                    MATERIEL_NAME = g.MATERIEL_NAME,
                    SUPPLIER_NAME = g.SUPPLIER_NAME,
                    TRANSPORT_NAME = g.TRANSPORT_NAME,
                    BILL_WEIGHT = g.BILL_WEIGHT,
                    GROSS = g.GROSS,
                    TARE = g.TARE,
                    NET = g.NET,
                    COEFFECENT = g.COEFFECENT,
                    BALANCE = g.BALANCE,
                    STATS_DAY = g.STATS_DAY,
                    STATS_TIME = g.STATS_TIME
                }).ToList();

            result = (from u in Temp
                      orderby u.PLAN_DATE, u.STATS_TIME ascending
                      select new V_RPT_RCM_DETAIL()
                      {
                          PLAN_DATE = u.PLAN_DATE,
                          CAR_ID = u.CAR_ID,
                          LPR = u.LPR,
                          MATERIEL_NAME = u.MATERIEL_NAME,
                          SUPPLIER_NAME = u.SUPPLIER_NAME,
                          TRANSPORT_NAME = u.TRANSPORT_NAME,
                          BILL_WEIGHT = u.BILL_WEIGHT,
                          GROSS = u.GROSS,
                          TARE = u.TARE,
                          NET = u.NET,
                          COEFFECENT = u.COEFFECENT,
                          BALANCE = u.BALANCE,
                          STATS_DAY = u.STATS_DAY
                      }).ToList();


            return result;
        }

        public List<V_RPT_RCM_DETAIL> QueryRCMDetail(int Year, int Month, int Date, string MaterielId, string SupplierId, string TransportId)
        {
            List<V_RPT_RCM_DETAIL> result = new List<V_RPT_RCM_DETAIL>();
            var queryData = HomeRepository.QueryRCMDetail();
            if (Date == 0)
            {
                var Temp = queryData.Where(p => p.STATS_YEAR == Year && p.STATS_MONTH == Month)
                    .OrderBy(o => o.SUPPLIER_NAME)
                    .GroupBy(x => new { x.MATERIEL_ID, x.MATERIEL_NAME, x.SUPPLIER_ID, x.SUPPLIER_NAME, x.TRANSPORT_ID, x.TRANSPORT_NAME })
                    .Select(g => new
                    {
                        MATERIEL_ID = g.Key.MATERIEL_ID,
                        MATERIEL_NAME = g.Key.MATERIEL_NAME,
                        SUPPLIER_ID = g.Key.SUPPLIER_ID,
                        SUPPLIER_NAME = g.Key.SUPPLIER_NAME,
                        TRANSPORT_ID = g.Key.TRANSPORT_ID,
                        TRANSPORT_NAME = g.Key.TRANSPORT_NAME,
                        BILL_WEIGHT = g.Sum(x => x.BILL_WEIGHT),
                        CUTBILL = g.Count(),
                        BALANCE = g.Sum(x => x.BALANCE)
                    }).ToList();

                result = (from u in Temp
                          orderby u.SUPPLIER_ID ascending
                          select new V_RPT_RCM_DETAIL()
                          {
                              MATERIEL_ID = u.MATERIEL_ID,
                              MATERIEL_NAME = u.MATERIEL_NAME,
                              SUPPLIER_ID = u.SUPPLIER_ID,
                              SUPPLIER_NAME = u.SUPPLIER_NAME,
                              TRANSPORT_ID = u.TRANSPORT_ID,
                              TRANSPORT_NAME = u.TRANSPORT_NAME,
                              BILL_WEIGHT = u.BILL_WEIGHT,
                              CUTBILL = u.CUTBILL,
                              BALANCE = u.BALANCE
                          }).ToList();

            }
            else
            {
                DateTime date = Convert.ToDateTime(string.Format("{0}/{1}/{2}", Year, Month, Date)).Date;
                IQueryable<V_RPT_RCM_DETAIL> data = null;
                if (!string.IsNullOrEmpty(MaterielId) && !string.IsNullOrEmpty(SupplierId) && !string.IsNullOrEmpty(TransportId))
                {
                    data = queryData.Where(p => p.PLAN_DATE == date && p.SUPPLIER_ID == SupplierId && p.MATERIEL_ID == MaterielId && p.TRANSPORT_ID == TransportId);
                }
                else
                {
                    data = queryData.Where(p => p.PLAN_DATE == date);
                }

                var Temp = data.OrderBy(o => new { o.MATERIEL_ID, o.SUPPLIER_ID, o.TRANSPORT_ID })
                    .Select(g => new
                    {
                        PLAN_DATE = g.PLAN_DATE,
                        CAR_ID = g.CAR_ID,
                        LPR = g.LPR,
                        MATERIEL_NAME = g.MATERIEL_NAME,
                        SUPPLIER_NAME = g.SUPPLIER_NAME,
                        TRANSPORT_NAME = g.TRANSPORT_NAME,
                        BILL_WEIGHT = g.BILL_WEIGHT,
                        GROSS = g.GROSS,
                        TARE = g.TARE,
                        NET = g.NET,
                        COEFFECENT = g.COEFFECENT,
                        BALANCE = g.BALANCE
                    }).ToList();

                result = (from u in Temp
                          orderby u.MATERIEL_NAME, u.SUPPLIER_NAME, u.TRANSPORT_NAME ascending
                          select new V_RPT_RCM_DETAIL()
                          {
                              PLAN_DATE = u.PLAN_DATE,
                              CAR_ID = u.CAR_ID,
                              LPR = u.LPR,
                              MATERIEL_NAME = u.MATERIEL_NAME,
                              SUPPLIER_NAME = u.SUPPLIER_NAME,
                              TRANSPORT_NAME = u.TRANSPORT_NAME,
                              BILL_WEIGHT = u.BILL_WEIGHT,
                              GROSS = u.GROSS,
                              TARE = u.TARE,
                              NET = u.NET,
                              COEFFECENT = u.COEFFECENT,
                              BALANCE = u.BALANCE
                          }).ToList();


            }
            return result;
        }

        public List<V_HP_RC_BLANCE> QueryRCBlanceByYear(int iYear)
        {
            var queryData = HomeRepository.QueryRCBlance();
            var Temp = queryData.Where(p => p.AXIS_YEAR == iYear)
                .OrderBy(o => o.AXIS_MONTH)
                .GroupBy(x => new { x.AXIS_YEAR, x.AXIS_MONTH })
                .Select(g => new
                {
                    AXIS_YEAR = g.Key.AXIS_YEAR,
                    AXIS_MONTH = g.Key.AXIS_MONTH,
                    BLANCE = g.Sum(x => x.BLANCE),
                    CARCOUNT = g.Sum(x => x.CARCOUNT)
                }).ToList();

            List<V_HP_RC_BLANCE> result = (from u in Temp
                                           orderby u.AXIS_MONTH ascending
                                           select new V_HP_RC_BLANCE()
                                           {
                                               AXIS_YEAR = u.AXIS_YEAR,
                                               AXIS_MONTH = u.AXIS_MONTH,
                                               BLANCE = u.BLANCE,
                                               CARCOUNT = u.CARCOUNT
                                           }).ToList();

            return result;
        }

        public List<V_HP_RC_BLANCE> QueryRCBlanceByMonth(int iYear, int iMonth)
        {
            var queryData = HomeRepository.QueryRCBlance();
            var Temp = queryData.Where(p => p.AXIS_YEAR == iYear && p.AXIS_MONTH == iMonth)
                .OrderBy(o => o.AXIS_DAY)
                .GroupBy(x => new { x.AXIS_DATE, x.AXIS_DAY })
                .Select(g => new
                {
                    AXIS_DATE = g.Key.AXIS_DATE,
                    AXIS_DAY = g.Key.AXIS_DAY,
                    BLANCE = g.Sum(x => x.BLANCE),
                    CARCOUNT = g.Sum(x => x.CARCOUNT)
                }).ToList();

            List<V_HP_RC_BLANCE> result = (from u in Temp
                                           orderby u.AXIS_DATE ascending
                                           select new V_HP_RC_BLANCE()
                                           {
                                               AXIS_DATE = u.AXIS_DATE,
                                               AXIS_DAY = u.AXIS_DAY,
                                               BLANCE = u.BLANCE,
                                               CARCOUNT = u.CARCOUNT
                                           }).ToList();

            return result;
        }

        public List<V_STATS_RC_PLAN_WEIGHTING> QueryRCPlanWeighting(int iYear, int iMonth, int iDay = 0)
        {
            if (iDay == 0)
                return HomeRepository.QueryRCPlanWeighting().Where(p => p.STATS_YEAR == iYear && p.STATS_MONTH == iMonth).OrderBy(x => x.STATS_MONTH).ToList();
            else
                return HomeRepository.QueryRCPlanWeighting().Where(p => p.STATS_YEAR == iYear && p.STATS_MONTH == iMonth && p.STATS_DAY == iDay).OrderBy(x => x.STATS_MONTH).ToList();
        }

        public List<V_STATS_RC_PLAN_WEIGHTING> QueryRCPlanWeighting(string MATERIEL_ID, string SUPPLIER_ID, string TRANSPORT_ID, int QueryYear, int QueryMonth)
        {
            return HomeRepository.QueryRCPlanWeighting().Where(p => p.MATERIEL_ID == MATERIEL_ID && p.SUPPLIER_ID == SUPPLIER_ID && p.TRANSPORT_ID == TRANSPORT_ID && p.STATS_YEAR == QueryYear && p.STATS_MONTH == QueryMonth).OrderBy(x => x.STATS_MONTH).ToList();
        }

        public List<V_RC_WEIGHING> QueryWeighting(string BATCH_CODE)
        {
            return HomeRepository.QueryRCWeighing().Where(p => p.BATCH_CODE == BATCH_CODE).ToList();
        }

        public List<V_RC_WEIGHING> QueryWeighting(string MATERIEL_ID, string SUPPLIER_ID, string TRANSPORT_ID, string LPR, int QueryYear, int QueryMonth)
        {
            DateTime dtStartTime = Convert.ToDateTime(string.Format("{0}-{1}-01 00:00:00", QueryYear, QueryMonth));
            DateTime dtEndTime = Convert.ToDateTime(string.Format("{0}-{1}-01 23:59:59", QueryYear, QueryMonth + 1)).AddDays(-1);

            return HomeRepository.QueryRCWeighing().Where(p => p.MATERIEL_ID == MATERIEL_ID && p.SUPPLIER_ID == SUPPLIER_ID && p.TRANSPORT_ID == TRANSPORT_ID && p.LPR == LPR && p.STATS_TIME >= dtStartTime && p.STATS_TIME <= dtEndTime).ToList();
        }

        public List<V_HP_COAL_STRUCTURE> QueryCoalStructure()
        {
            return HomeRepository.QueryCoalStructure().ToList();
        }

        public List<V_HP_LMS> QueryLms(int QueryYear, int RTYPE)
        {
            if (QueryYear == DateTime.Now.Year)
            {
                return HomeRepository.QueryLms().Where(p => p.AXIS_YEAR == QueryYear && p.RTYPE == RTYPE && p.AXIS_MONTH <= DateTime.Now.Month).ToList();
            }
            else
            {
                return HomeRepository.QueryLms().Where(p => p.AXIS_YEAR == QueryYear && p.RTYPE == RTYPE).ToList();
            }
        }

        public List<V_RPT_LMS_ALL_MONTH> QueryLmsAll(int QueryYear, string RTYPE)
        {
            return HomeRepository.QueryLmsAll().Where(p => p.RPT_YEAR == QueryYear && p.INDEX_ID == RTYPE).ToList();

        }

        public RPT_LMS_RZC QueryLmsRZC()
        {
            return HomeRepository.QueryLmsRZC().Where(p => p.RPT_YEAR == DateTime.Now.Year).ToList().FirstOrDefault();
        }
    }
}
